package bb.lanxing.lib.devices.common;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
public class BroadcastLogger implements DeviceLogger {
    private static final String ACTION_DEVICE_LOG = "bb.lanxing.lib.devices.log";
    private static final String EXTRA_MESG = "extra_mesg";
    private static final String EXTRA_TAG = "extra_tag";
    private static final String EXTRA_TYPE = "extra_type";
    private Context context;

    public BroadcastLogger(Context context) {
        this.context = context;
    }

    @Override
    public void d(String str, String str2) {
        sendBroadcastLog(3, str, str2);
    }

    @Override
    public void e(String str, String str2) {
        sendBroadcastLog(6, str, str2);
    }

    @Override
    public void e(String str, Throwable th) {
        e(str, Log.getStackTraceString(th));
    }

    protected void sendBroadcastLog(int i, String str, String str2) {
        Intent intent = new Intent(ACTION_DEVICE_LOG);
        intent.putExtra(EXTRA_TAG, str);
        intent.putExtra(EXTRA_MESG, str2);
        intent.putExtra("extra_type", i);
        intent.setPackage(this.context.getPackageName());
        this.context.sendBroadcast(intent);
    }


    public static abstract class LogReceiver extends BroadcastReceiver {
        protected abstract void d(String str, String str2);

        protected abstract void e(String str, String str2);

        public void registerBroadcast(Context context) {
            IntentFilter intentFilter = new IntentFilter();
            intentFilter.addAction(BroadcastLogger.ACTION_DEVICE_LOG);
            context.registerReceiver(this, intentFilter);
        }

        public void unregisterBroadcast(Context context) {
            context.unregisterReceiver(this);
        }

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(BroadcastLogger.ACTION_DEVICE_LOG)) {
                String stringExtra = intent.getStringExtra(BroadcastLogger.EXTRA_TAG);
                String stringExtra2 = intent.getStringExtra(BroadcastLogger.EXTRA_MESG);
                int intExtra = intent.getIntExtra("extra_type", 6);
                if (intExtra == 3) {
                    d(stringExtra, stringExtra2);
                } else if (intExtra != 6) {
                } else {
                    e(stringExtra, stringExtra2);
                }
            }
        }
    }
}
